package net.dopan.pigframe.user.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.dopan.pigframe.core.base.controller.BaseController;
import net.dopan.pigframe.core.utils.CommonConstant;
import net.dopan.pigframe.user.model.dto.DeptTree;
import net.dopan.pigframe.user.model.entity.SysDept;
import net.dopan.pigframe.user.service.SysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Package: net.dopan.pigframe.user.controller
 * @Title: DeptController
 * @Description: 部门管理
 * @author: 刘宽
 * @date: 2018/9/2 21:27
 */
@RestController
@RequestMapping("/dept")
@Api(tags = "部门管理接口(API)", description = "部门管理相关操作")
public class DeptController extends BaseController {

    @Autowired
    private SysDeptService sysDeptService;

    /**
     * 通过ID查询
     *
     * @param id ID
     * @return SysDept
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询部门", notes = "通过ID查询部门")
    public SysDept get(@PathVariable Integer id) {
        return sysDeptService.selectById(id);
    }


    /**
     * 返回树形菜单集合
     *
     * @return 树形菜单
     */
    @GetMapping(value = "/tree")
    @ApiOperation(value = "返回部门树形菜单集合", notes = "返回部门树形菜单集合")
    public List<DeptTree> getTree() {
        SysDept condition = new SysDept();
        condition.setDelFlag(CommonConstant.STATUS_NORMAL);
        return sysDeptService.selectListTree(new EntityWrapper<>(condition));
    }

    /**
     * 添加
     *
     * @param sysDept 实体
     * @return success/false
     */
    @PostMapping
    @ApiOperation(value = "添加部门", notes = "添加部门")
    public Boolean add(@RequestBody SysDept  sysDept) {
        return sysDeptService.insertDept(sysDept);
    }

    /**
     * 删除
     *
     * @param id ID
     * @return success/false
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除部门", notes = "删除部门")
    public Boolean delete(@PathVariable Integer id) {
        return sysDeptService.deleteDeptById(id);
    }

    /**
     * 编辑
     *
     * @param sysDept 实体
     * @return success/false
     */
    @PutMapping
    @ApiOperation(value = "编辑部门", notes = "编辑部门")
    public Boolean edit(@RequestBody SysDept sysDept) {
        sysDept.setUpdateTime(new Date());
        return sysDeptService.updateDeptById(sysDept);
    }
}
